package com.bailan.foreat.config;

import com.bailan.foreat.interceptors.JwtInterceptor;
import com.bailan.foreat.interceptors.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    JwtInterceptor jwtInterceptor;

    @Autowired
    PermissionInterceptor permissionInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //WebMvcConfigurer.super.addViewControllers(registry);

        registry.addViewController("/").setViewName("homeh");
        registry.addViewController("/productPage.html").setViewName("productPage");
        registry.addViewController("/allStore.html").setViewName("allStore");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/register.html").setViewName("register");
        registry.addViewController("/center.html").setViewName("center");
        registry.addViewController("/myStore.html").setViewName("myStore");
        registry.addViewController("/addStore.html").setViewName("addStore");
        registry.addViewController("/updateStore.html").setViewName("updateStore");
        registry.addViewController("/password.html").setViewName("password");
        registry.addViewController("/sellerImg.html").setViewName("sellerImg");
        registry.addViewController("/sellerorder.html").setViewName("sellerorder");



    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册interceptor
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/**");

        List<String> exclude = new ArrayList<>();
        exclude.add("/");
        exclude.add("/**/*home*/**");
        exclude.add("/allStore.html");
        exclude.add("/productdetail");
        exclude.add("/storemenu");
        exclude.add("/static/**");
        exclude.add("/**/*seller*/**");
        exclude.add("/common/**");
        exclude.add("/**/*select*/**");
        exclude.add("/**/to*/**");
        exclude.add("/**/*log*/**");
        exclude.add("/**/*reg*/**");
        exclude.add("/productType/**");
        exclude.add("/storeType/**");
        exclude.add("/product/queryProductDetail");
        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**").excludePathPatterns(exclude);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //添加资源处理器路径 即每次访问静态资源都得添加"/static/",例如localhost:8080/static/j1.jpg
        //若registry.addResourceHandler("/s/**") 则必须访问localhost:8080/s/j1.jpg
        registry.addResourceHandler("/static/**")
                //添加了资源处理器路径后对应的映射资源路径
                .addResourceLocations("classpath:/static/");
    }

}
